/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.cloudfactory.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;

import com.alibaba.druid.support.json.JSONUtils;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.service.CrudService;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.common.utils.UserUtil;
import com.thinkgem.jeesite.modules.cloudfactory.entity.CloudfactoryBuycar;
import com.thinkgem.jeesite.modules.cloudfactory.entity.CloudfactoryFunction;
import com.thinkgem.jeesite.modules.sys.entity.User;
import com.thinkgem.jeesite.modules.cloudfactory.dao.CloudfactoryBuycarDao;
import com.thinkgem.jeesite.modules.cloudfactory.dao.CloudfactoryFunctionDao;

/**
 * 单表生成Service
 * @author ThinkGem
 * @version 2018-09-09
 */
@Service
@Transactional
public class CloudfactoryBuycarService extends CrudService<CloudfactoryBuycarDao, CloudfactoryBuycar> {
	@Autowired
	private CloudfactoryFunctionDao cloudfactoryFunctionDao;
	@Autowired
	private CloudfactoryBuycarDao cloudfactoryBuycarDao;
	public CloudfactoryBuycar get(String id) {
		return super.get(id);
	}
	
	public List<CloudfactoryBuycar> findList(CloudfactoryBuycar cloudfactoryBuycar) {
		return super.findList(cloudfactoryBuycar);
	}
	
	public Page<CloudfactoryBuycar> findPage(Page<CloudfactoryBuycar> page, CloudfactoryBuycar cloudfactoryBuycar) {
		return super.findPage(page, cloudfactoryBuycar);
	}
	
	@Transactional(readOnly = false)
	public void save(CloudfactoryBuycar cloudfactoryBuycar) {
		super.save(cloudfactoryBuycar);
	}
	
	@Transactional(readOnly = false)
	public void delete(CloudfactoryBuycar cloudfactoryBuycar) {
		super.delete(cloudfactoryBuycar);
	}

	public String saveBuyCar(HttpServletRequest request, Model model, String id, String orderNo) {
		Map<String,Object> map = new HashMap<String,Object>();
		
		
		
		 CloudfactoryFunction cloudfactoryFunction= cloudfactoryFunctionDao.get(id);
		 CloudfactoryBuycar  cloudfactoryBuycar = new CloudfactoryBuycar();
		 cloudfactoryBuycar.setMenuId(cloudfactoryFunction.getMenuId());
		 cloudfactoryBuycar.setIsRequired(cloudfactoryFunction.getIsRequired());
		 cloudfactoryBuycar.setFunctionId(id);
		 cloudfactoryBuycar.setUseTimes("1");
		 cloudfactoryBuycar.setOrderNo(orderNo);
		 cloudfactoryBuycar.setFunctionName(cloudfactoryFunction.getFunctionName());
		 User user =new User();
		 user.setId(UserUtil.getSessionUser(request).getId());
		 cloudfactoryBuycar.setUser(user);
		 this.save(cloudfactoryBuycar);
		 List<CloudfactoryBuycar> loudfactoryBuycar = this.findList(cloudfactoryBuycar);
		 String data = "";
		 String dele = "";
		 String useTime = "";
		 Integer totalMoney = 0;
		 for(CloudfactoryBuycar c : loudfactoryBuycar) {
			 data = data+"<li>"+c.getFunctionName()+"</li><br>";
			 dele = dele+"<li class=\"tc-fff\" onclick=\"deleBuyCar('"+c.getId()+"','"+c.getOrderNo()+"')\">删除</li><br>";
			 useTime = useTime + 
					 "<li><select id=\""+c.getId()+"\" onChange=\"getChangeUseTime('"+c.getId()+"')\" name=\"duration\"><option value=\"1\">一个月</option><option value=\"6\">六个月</option><option value=\"12\">十二个月</option> </select></li><br>";
			 
			 String functionId = c.getFunctionId();
			 if(!StringUtils.isBlank(functionId)) {
				 
				 CloudfactoryFunction function = cloudfactoryFunctionDao.get(functionId);
				 String usetime = c.getUseTimes();
				 Integer ut = 0;
				 if(!StringUtils.isBlank(usetime)) {
					 ut = Integer.valueOf(usetime);
				 }
				 if(function != null) {
					 if(!StringUtils.isBlank(function.getMoney())) {
					 totalMoney = totalMoney+Integer.valueOf(!StringUtils.isBlank(function.getMoney())?function.getMoney():"0")*ut;
					 }
				 }
			 }
		 }
		 
		 map.put("data", data);
		 map.put("dele", dele);
		 map.put("usetime",useTime);
		 map.put("money",  "¥ "+totalMoney+" 元");
		return JSONUtils.toJSONString(map);
	}
	public String deleBuyCar(HttpServletRequest request, Model model, String id, String orderNo) {
		CloudfactoryBuycar buyCar = this.get(id);
		this.delete(buyCar);
		CloudfactoryBuycar  cloudfactoryBuycar = new CloudfactoryBuycar();
		User user =new User();
		 user.setId(UserUtil.getSessionUser(request).getId());
		 cloudfactoryBuycar.setUser(user);
		 cloudfactoryBuycar.setOrderNo(orderNo);
		 List<CloudfactoryBuycar> loudfactoryBuycar = this.findList(cloudfactoryBuycar);
		 String data = "";
		 String dele = "";
		 String useTime = "";
		 Integer totalMoney = 0;
		 for(CloudfactoryBuycar c : loudfactoryBuycar) {
			 data = data+"<li>"+c.getFunctionName()+"</li><br>";
			 dele = dele+"<li class=\"tc-fff\" onclick=\"deleBuyCar('"+c.getId()+"','"+c.getOrderNo()+"')\">删除</li><br>";
			 useTime = useTime + 
					 "<li class=\"tc-fff\"><select id=\""+c.getId()+"\" onChange=\"getChangeUseTime('"+c.getId()+"')\" name=\"duration\"><option value=\"1\">一个月</option><option value=\"2\">六个月</option><option value=\"3\">十二个月</option> </select></li><br>";
			 
			 String functionId = c.getFunctionId();
			 if(!StringUtils.isBlank(functionId)) {
				 
				 CloudfactoryFunction function = cloudfactoryFunctionDao.get(functionId);
				 String usetime = c.getUseTimes();
				 Integer ut = 0;
				 if(!StringUtils.isBlank(usetime)) {
					 ut = Integer.valueOf(usetime);
				 }
				 if(function != null) {
					 if(!StringUtils.isBlank(function.getMoney())) {
					 totalMoney = totalMoney+Integer.valueOf(!StringUtils.isBlank(function.getMoney())?function.getMoney():"0")*ut;
					 }
				 }
			 }
		 }
		 Map<String,Object> map = new HashMap<String,Object>();
		 map.put("data", data);
		 map.put("usetime", useTime);
		 map.put("money","¥ "+totalMoney+" 元");
		 if(loudfactoryBuycar == null || loudfactoryBuycar.size()==0) {
			 map.put("dele", "");
		 } else {
		 map.put("dele", dele);
		 }
		return JSONUtils.toJSONString(map);
	}

	public String changeUseTime(String id, String times,HttpServletRequest request) {
		CloudfactoryBuycar buycar = cloudfactoryBuycarDao.get(id);
		buycar.setUseTimes(times);
		
		cloudfactoryBuycarDao.update(buycar);
		 List<CloudfactoryBuycar> loudfactoryBuycar = this.findList(buycar);
		 String data = "";
		 String dele = "";
		 String useTime = "";
		 Integer totalMoney = 0;
		 for(CloudfactoryBuycar c : loudfactoryBuycar) {
			 data = data+"<li>"+c.getFunctionName()+"</li><br>";
			 dele = dele+"<li class=\"tc-fff\" onclick=\"deleBuyCar('"+c.getId()+"','"+c.getOrderNo()+"')\">删除</li><br>";
			 useTime = useTime + 
					 "<li><select id=\""+c.getId()+"\" onChange=\"getChangeUseTime('"+c.getId()+"')\" name=\"duration\"><option value=\"1\">一个月</option><option value=\"6\">六个月</option><option value=\"12\">十二个月</option> </select></li><br>";
			 
			 String functionId = c.getFunctionId();
			 if(!StringUtils.isBlank(functionId)) {
				 
				 CloudfactoryFunction function = cloudfactoryFunctionDao.get(functionId);
				 String usetime = c.getUseTimes();
				 Integer ut = 0;
				 if(!StringUtils.isBlank(usetime)) {
					 ut = Integer.valueOf(usetime);
				 }
				 if(function != null) {
					 if(!StringUtils.isBlank(function.getMoney())) {
					 totalMoney = totalMoney+Integer.valueOf(!StringUtils.isBlank(function.getMoney())?function.getMoney():"0")*ut;
					 }
				 }
			 }
		 }
		 Map<String,Object> map = new HashMap<String,Object>();
		 map.put("data", data);
		 map.put("dele", dele);
		 map.put("usetime",useTime);
		 map.put("money",  "¥ "+totalMoney+" 元");
		return JSONUtils.toJSONString(map);
		
	}
}